package com.carlyang.ajaxtest;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class GetAjaxInfoServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public GetAjaxInfoServlet() {
		super();
		creatUser();
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		String action = request.getParameter("action");
		if("sampleJsonObject".equals(action)){
			this.getSampleJsonObject(request, response);
		}else if ("complexJsonObject".equals(action)) {
			this.getComplexJsonObject(request, response);
		}else if ("nestedJsonObject".equals(action)) {
			this.getNestedJsonObject(request, response);
		}else if ("checkUserName".equals(action)) {
			this.checkUserName(request, response);
		}else if ("ejld".equals(action)) {
			this.ejld(request, response);
		}
	}
	//返回简单json数据
	//{"name":"张三","age":"23"}
	 
	private void getSampleJsonObject(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		//String resultJson = "{\"name\":\"张三\",\"age\":\"23\"}";//手动拼接JSON字符串，麻烦
		JSONObject resultJson = new JSONObject();//使用JSON的API jar包
		resultJson.put("name", "张三");
		resultJson.put("age", "25");
		//服务端用response回应客户端
		
		PrintWriter out = response.getWriter();
		out.println(resultJson);
		out.flush();
		out.close();
	}
	/**
	 * 返回较为复杂的json数据
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 
	 JSON 数组
	 {
	 	"students":[
	 		{"name":"张三","age":"23"}
	 		{"name":"李四","age":"25"}
	 		{"name":"王五","age":"26"}
	 	]
	 } 
	 */
	private void getComplexJsonObject(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		JSONObject resultJson = new JSONObject();//使用JSON的API jar包
		JSONArray jsonArray = new JSONArray();
		JSONObject jsonObject1 = new JSONObject();
		JSONObject jsonObject2 = new JSONObject();
		JSONObject jsonObject3 = new JSONObject();
		jsonObject1.put("name", "张三");
		jsonObject1.put("age", "23");
		jsonObject2.put("name", "李四");
		jsonObject2.put("age", "25");
		jsonObject3.put("name", "王五");
		jsonObject3.put("age", "26");
		jsonArray.add(jsonObject1);
		jsonArray.add(jsonObject2);
		jsonArray.add(jsonObject3);
		resultJson.put("students", jsonArray);//将JSON数组放到JSON对象中
		
		//服务端用response回应客户端
		PrintWriter out = response.getWriter();
		out.println(resultJson);
		out.flush();
		out.close();
	}
	
	private void getNestedJsonObject(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		JSONObject resultJson = new JSONObject();//使用JSON的API jar包
		JSONArray jsonArray = new JSONArray();
		JSONObject jsonObject1 = new JSONObject();
		jsonObject1.put("name", "张三");
		jsonObject1.put("age", "23");
		JSONObject scoreObject1 = new JSONObject();
		scoreObject1.put("语文", "80");
		scoreObject1.put("数学", "90");
		scoreObject1.put("英语", "93");
		jsonObject1.put("score", scoreObject1);
		jsonArray.add(jsonObject1);
		
		JSONObject jsonObject2 = new JSONObject();
		jsonObject2.put("name", "李四");
		jsonObject2.put("age", "25");
		JSONObject scoreObject2 = new JSONObject();
		scoreObject2.put("语文", "85");
		scoreObject2.put("数学", "90");
		scoreObject2.put("英语", "95");
		jsonObject2.put("score", scoreObject2);
		jsonArray.add(jsonObject2);
		
		JSONObject jsonObject3 = new JSONObject();
		jsonObject3.put("name", "王五");
		jsonObject3.put("age", "26");
		JSONObject scoreObject3 = new JSONObject();
		scoreObject3.put("语文", "90");
		scoreObject3.put("数学", "100");
		scoreObject3.put("英语", "85");
		jsonObject3.put("score", scoreObject3);
		jsonArray.add(jsonObject3);
		
		resultJson.put("students", jsonArray);//将JSON数组放到JSON对象中
		
		//服务端用response回应客户端
		PrintWriter out = response.getWriter();
		out.println(resultJson);
		out.flush();
		out.close();
	}
	
	class User{
		private String id;
		private String name;
		private String password;
		public User() {
			super();
		}
		public User(String id, String name, String password) {
			super();
			this.id = id;
			this.name = name;
			this.password = password;
		}
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getPassword() {
			return password;
		}
		public void setPassword(String password) {
			this.password = password;
		}
		@Override
		public String toString() {
			return "user [id=" + id + ", name=" + name + ", password=" + password + "]";
		}
	}	


	private List<User> userList= new ArrayList<>();
	public void creatUser() {
		userList.add(new User("1","carlyang","123456"));
		userList.add(new User("2","test","123456"));
		userList.add(new User("3","杨东","123456"));
	}
	//
	private void checkUserName(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		JSONObject resultJson = new JSONObject();//使用JSON的API jar包
		resultJson.put("exist", 1);
		String userName = request.getParameter("userName");
		System.out.println("传入的参数userName："+userName);
		if(userName.equals("")){
			System.out.println("传入的参数为空字符串！");
			resultJson.put("exist", -1);
		}else if (userName.contains("*")||userName.contains("/")||userName.contains("\\")) {
			System.out.println("传入的参数带有非法字符！");	
			resultJson.put("exist", -2);
		}else {
			Iterator<User> iterator = userList.iterator();
			while (iterator.hasNext()) {
				User user = (User) iterator.next();
				if(user.getName().equals(userName)){
					resultJson.put("exist", 0);
					System.out.println("用户"+userName+"存在！");
					break;
				}
			}
		}
		//服务端用response回应客户端
		PrintWriter out = response.getWriter();
		out.println(resultJson);
		out.flush();
		out.close();
	}
	//
	private void ejld(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		JSONObject resultJson = new JSONObject();//使用JSON的API jar包
		String shengId = request.getParameter("shengId");
		System.out.println("传入的参数--省份："+shengId);
		JSONArray jsonArray = new JSONArray();//JSON对象数组
		JSONObject[] tempJson = new JSONObject[10];//临时Json对象引用
		//未添加数据库操作，使用模拟值，
		switch (Integer.parseInt(shengId)) {
		case 0:{//北京
			tempJson[0] = new JSONObject();
			tempJson[0].put("id", 1);
			tempJson[0].put("name", "北京");
		}
			break;
		case 1:{//江苏
			tempJson[0] = new JSONObject();tempJson[0].put("id", 1);tempJson[0].put("name", "南京");
			tempJson[1] = new JSONObject();tempJson[1].put("id", 2);tempJson[1].put("name", "苏州");
		}
			break;
		case 2:{//辽宁
			tempJson[0] = new JSONObject();tempJson[0].put("id", 1);tempJson[0].put("name", "沈阳");
			tempJson[1] = new JSONObject();tempJson[1].put("id", 2);tempJson[1].put("name", "大连");
			tempJson[2] = new JSONObject();tempJson[2].put("id", 3);tempJson[2].put("name", "秦皇岛");
		}
			break;
		case 3:{//江西
			tempJson[0] = new JSONObject();tempJson[0].put("id", 1);tempJson[0].put("name", "南昌");
			tempJson[1] = new JSONObject();tempJson[1].put("id", 2);tempJson[1].put("name", "赣州");
			tempJson[2] = new JSONObject();tempJson[2].put("id", 3);tempJson[2].put("name", "九江");
		}
			break;
		case 4:{//福建
			tempJson[0] = new JSONObject();tempJson[0].put("id", 1);tempJson[0].put("name", "福州");
			tempJson[1] = new JSONObject();tempJson[1].put("id", 2);tempJson[1].put("name", "厦门");
			tempJson[2] = new JSONObject();tempJson[2].put("id", 3);tempJson[2].put("name", "三明");
		}
			break;
		case 5:{//广东
			tempJson[0] = new JSONObject();tempJson[0].put("id", 1);tempJson[0].put("name", "广州");
			tempJson[1] = new JSONObject();tempJson[1].put("id", 2);tempJson[1].put("name", "深圳");
			tempJson[2] = new JSONObject();tempJson[2].put("id", 3);tempJson[2].put("name", "惠州");
		}
			break;
		default:
			break;
		}
		for (int i = 0; i < tempJson.length; i++) {
			if (tempJson[i]!=null) {
				//System.out.println("tempJson["+i+"]="+tempJson[i]);
				jsonArray.add(tempJson[i]);
			}
		}
		resultJson.put("citiesName", jsonArray);
		//服务端用response回应客户端
		PrintWriter out = response.getWriter();
		out.println(resultJson);
		out.flush();
		out.close();
	}
}
